<template>
  <div class="login-container">
    <div class="login-b">
     

      <div class="login_box">
        <!-- 登陆表单 -->
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginFormRules"
          label-width="0px"
        >
          <!-- 用户名 -->
          <div class="int">
          <el-form-item class="input" prop="username">
            <el-input 
              v-model="loginForm.username"
              prefix-icon="el-icon-s-custom"
              clearable
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="passname">
            <el-input 
              v-model="loginForm.passname"
              prefix-icon="el-icon-unlock"
              type="password"
              show-password
              @keyup.enter="login"
            ></el-input>
          </el-form-item>
          </div>
          <el-form-item class="btns">
            <el-button type="primary" @click="login" >登录</el-button>
            <el-button type="info" @click="resetLoginForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  name:'UserLogin',
  data() {
    return {
      loginForm: {
        username: '',
        passname: '',
      },
      // 表单验证规则
      loginFormRules: {
        // 验证用户合法
        username: [
          {
            required: true,
            message: '请输入账号',
            trigger: 'blur',
          },
          { min: 3, max: 10, message: '长度在 3 到10 个字符', trigger: 'blur' },
        ],
        // 验证密码合法
        passname: [
          {
            required: true,
            message: '请输入密码 ',
            trigger: 'blur',
          },
          {
            min: 2,
            max: 15,
            message: '长度在 2 到15 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    ...mapActions(['setUserSync']),
    // 重置表单数据
    resetLoginForm() {
      this.loginForm.username = '',
      this.loginForm.passname = ''
    
    },
    login() {
      //  this.$refs.loginFormRef.validate(valid =>{
      //   //  console.log(valid);
      //   if(!valid) return;
      //   this.$
      //  });
      let wanGo = this.$route.query.redirect
      let g = wanGo ? wanGo : '/'  //如果之前想访问的页面存在 就访问否侧就是'/'
      console.log('想去的路径query',g);
      this.setUserSync({
        username: this.loginForm.username,
        // password: this.$md5(this.loginForm.passname),
        password: this.loginForm.passname,
                  
        
      })
        .then((res) => {
          console.log('调用actions成功：', res)
          localStorage.setItem('token',res.value.data.token)//将token存入本地
          this.$router.push(g)
          return this.$message.success('登陆成功！')
        })
        .catch((err) => {
          console.log('调用actions失败：', err)
          return this.$message.error('登录失败！')
        })
    },
  },
}
</script>

<style lang="less" scoped>
.btns {
  display: flex;
  justify-content: flex-end;
  transform: translate(-35%);
}
.input {
  margin-top: 60px;
}
.int {
  width: 370px;

  margin: 0 auto;
  display: bl;
}


</style>